<template>
  <div v-loading="loading">
    <div style="margin-top: 10px;display: flex;justify-content: center">
      <el-input
        placeholder="默认展示部分用户，可以通过用户名搜索用户..."
        prefix-icon="el-icon-search"
        v-model="keywords"
        style="width: 400px"
        size="small"
      >
      </el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="small"
        style="margin-left: 3px"
        @click="searchClick"
        >搜索
      </el-button>
    </div>
    <div style="display: flex;justify-content: space-around;flex-wrap: wrap">
      <el-card
        style="width:330px;margin-top: 10px;"
        v-for="(user, index) in users"
        :key="index"
        v-loading="cardloading[index]"
      >
        <div slot="header" style="text-align: left">
          <span>{{ user.nickName }}</span>
          <el-button
            style="float: right; padding: 3px 0;color: #ff0509"
            type="text"
            icon="el-icon-delete"
            @click="deleteUser(user.id)"
            >删除
          </el-button>
        </div>
        <div>
          <div>
            <img
              :src="user.userFace"
              :alt="user.nickname"
              style="width: 70px;height: 70px"
            />
          </div>
          <div
            style="text-align: left;color:#20a0ff;font-size: 12px;margin-top: 13px"
          >
            <span>用户名:</span><span>{{ user.username }}</span>
          </div>
          <div
            style="text-align: left;color:#20a0ff;font-size: 12px;margin-top: 13px"
          >
            <span>电子邮箱:</span><span>{{ user.email }}</span>
          </div>
          <div
            style="text-align: left;color:#20a0ff;font-size: 12px;margin-top: 13px"
          >
            <span>手机号:</span><span>{{ user.mobile }}</span>
          </div>
          <div
            style="text-align: left;color:#20a0ff;font-size: 12px;margin-top: 13px"
          >
            <span>注册时间:</span><span>{{ user.createTime }}</span>
          </div>
          <div
            style="text-align: left;color:#20a0ff;font-size: 12px;margin-top: 13px;display: flex;align-items: center"
          >
            <span>用户状态:</span>
            <el-switch
              v-model="user.enabled"
              active-text="启用"
              active-color="#13ce66"
              @change="enabledChange(user.enabled, user.id, index)"
              inactive-text="禁用"
              style="font-size: 12px"
            >
            </el-switch>
          </div>
          <div
            style="text-align: left;color:#20a0ff;font-size: 12px;margin-top: 13px"
          >
            <span>用户角色:</span>
            <el-tag
              v-for="role in user.roles"
              :key="role.id"
              size="mini"
              style="margin-right: 8px"
              type="success"
            >
              {{ role.zhName }}
            </el-tag>
            <el-popover
              placement="right"
              title="角色列表"
              width="200"
              :key="index + '' + user.id"
              @hide="saveRoles(user.id, index)"
              trigger="click"
              v-loading="eploading[index]"
            >
              <el-select
                v-model="roles"
                :key="user.id"
                multiple
                placeholder="请选择"
                size="mini"
              >
                <el-option
                  v-for="item in allRoles"
                  :key="user.id + '-' + item.id"
                  :label="item.zhName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
              <el-button
                type="text"
                icon="el-icon-more"
                style="padding-top: 0px"
                slot="reference"
                @click="showRole(user.roles, user.id, index)"
              ></el-button>
            </el-popover>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { get, put, del } from "../utils/api";
export default {
  data() {
    return {
      loading: false,
      eploading: [],
      cardloading: [],
      keywords: "",
      users: [],
      allRoles: [],
      roles: [],
      cpRoles: []
    };
  },
  mounted() {
    this.loading = true;
    this.loadUserList();
    this.cardloading = Array.apply(null, Array(20)).map(function(item, i) {
      console.log(item, i);
      return false;
    });
    this.eploading = Array.apply(null, Array(20)).map(function(item, i) {
      console.log(item, i);
      return false;
    });
  },
  methods: {
    saveRoles(id, index) {
      var selRoles = this.roles;
      if (this.cpRoles.length == selRoles.length) {
        for (var i = 0; i < this.cpRoles.length; i++) {
          for (var j = 0; j < selRoles.length; j++) {
            if (this.cpRoles[i].id == selRoles[j]) {
              selRoles.splice(j, 1);
              break;
            }
          }
        }
        if (selRoles.length == 0) {
          return;
        }
      }

      this.cardloading.splice(index, 1, true);
      put("/admin/user/role", { rids: this.roles, id: id }).then(resp => {
        if (resp.code == 0) {
          this.$message({ type: resp.data.status, message: resp.data.msg });
          this.loadOneUserById(id, index);
        } else {
          this.cardloading.splice(index, 1, false);
          this.$message({ type: "error", message: "更新失败!" });
        }
      });
    },
    showRole(aRoles, id, index) {
      this.cpRoles = aRoles;
      this.roles = [];
      this.loadRoles(index);
      for (var i = 0; i < aRoles.length; i++) {
        this.roles.push(aRoles[i].id);
      }
    },
    deleteUser(id) {
      this.$confirm("删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del("/admin/user/" + id).then(resp => {
            if (resp.code == 0) {
              this.$message({ type: "success", message: "删除成功!" });
              this.loadUserList();
              return;
            }
            this.loading = false;
            this.$message({ type: "error", message: "删除失败!" });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    enabledChange(enabled, id, index) {
      this.cardloading.splice(index, 1, true);
      put("/admin/user/enabled", { enabled: enabled, id: id }).then(resp => {
        if (resp.code != 0) {
          this.$message({ type: "error", message: "更新失败!" });
          this.loadOneUserById(id, index);
          return;
        }
        this.cardloading.splice(index, 1, false);
        this.$message({ type: "success", message: "更新成功!" });
      });
    },
    loadRoles(index) {
      this.eploading.splice(index, 1, true);
      get("/admin/roles").then(resp => {
        this.eploading.splice(index, 1, false);
        if (resp.code == 0) {
          this.allRoles = resp.data;
        } else {
          this.$message({ type: "error", message: "数据加载失败!" });
        }
      });
    },
    loadOneUserById(id, index) {
      get("/admin/user/" + id).then(resp => {
        this.cardloading.splice(index, 1, false);
        if (resp.code == 0) {
          this.users.splice(index, 1, resp.data);
        } else {
          this.$message({ type: "error", message: "数据加载失败!" });
        }
      });
    },
    loadUserList() {
      get("/admin/users?keyword=" + this.keywords).then(resp => {
        this.loading = false;
        if (resp.code == 0) {
          this.users = resp.data;
        } else {
          this.$message({ type: "error", message: "数据加载失败!" });
        }
      });
    },
    searchClick() {
      this.loading = true;
      this.loadUserList();
    }
  }
};
</script>

<style></style>
